package com.njlime.cms.repository.app;

import com.njlime.cms.entities.Live;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import com.njlime.core.utils.StringUtil;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 直播
 */
@Repository
public class AppLiveDaoImpl extends BaseDaoImpl<Live> {


    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    public Map<String, Object> get(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_live where id  = ?");
        arrList.add(id);
        List<Map<String, Object>> re = getListJdbc(sb.toString(), arrList);
        if (re != null && re.size() > 0) {
            return re.get(0);
        }
        return null;
    }

    /**
     * 直播列表
     * @param pageBean
     * @param tLive
     * @return
     */
    public PageBean list(PageBean pageBean, Live tLive) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select liv.* from t_live liv where liv.is_del !=1 ");
        if (!StringUtil.isEmpty(tLive.getTitle())) {
            sb.append(" and liv.title like concat('%',?,'%') ");
            arrList.add(tLive.getTitle());
        }
        if (tLive.getPublish_status() != null) {
            sb.append(" and liv.publish_status = ? ");
            arrList.add(tLive.getPublish_status());
        }
        if (tLive.getChannel_id() != null) {
            sb.append(" and liv.channel_id = ? ");
            arrList.add(tLive.getChannel_id());
        }
        if (tLive.getUnit_id() != null) {
            sb.append(" and liv.unit_id = ? ");
            arrList.add(tLive.getUnit_id());
        }
        if (tLive.getRoot_unit_id() != null) {
            sb.append(" and liv.root_unit_id = ? ");
            arrList.add(tLive.getRoot_unit_id());
        }
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by liv.position asc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 改变评论
     * @return
     */
    public void updateCommentMum(Integer id,int num) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_live SET comment_num = comment_num + ? WHERE id = ?");
        arrList.add(num);
        arrList.add(id);
        updateJdbc(sb.toString(), arrList);
    }

    /**
     * 改变观看人数
     * @return
     */
    public void updateViewCount(Integer id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_live SET reading = IFNULL(reading,0) + 1 WHERE id = ?");
        arrList.add(id);
        updateJdbc(sb.toString(), arrList);
    }
}
